package es.ucm.pe.GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import es.ucm.pe.algorithms.Cromosoma;
import es.ucm.pe.algorithms.Distancias;

public class MejorIndividuo extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	public MejorIndividuo(Cromosoma c){
		super();
		this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
		this.add(Box.createVerticalBox());
		
		JLabel jLabel1 = new JLabel("Aptitud: " + c.getAptitud());
		JLabel jLabel2 = new JLabel("Puntuacion: " + c.getPuntuacion());
		JLabel jLabel3 = new JLabel("GEN");
		JSeparator jSeparator1 = new JSeparator(SwingConstants.HORIZONTAL);
		this.add(jLabel1);
		this.add(jLabel2);
		this.add(new JLabel("  "));
		this.add(jLabel3);
		this.add(jSeparator1);
		JPanel j = new JPanel();
		j.setLayout(new GridBagLayout());
		ArrayList<GridBagConstraints> grids1 = new ArrayList<GridBagConstraints>();
		
		int numColumna = 0;
		int fila = 0;
		
		GridBagConstraints grid = new GridBagConstraints();
		grid.gridx = 0;
		grid.gridy = 0;
		j.add(new JLabel("Ciudad 1: "  + Distancias.CIUDADES[0]),grid);
		
		int aux = 0;
		
		for(int i = 1; i < c.getGen().length+1; i++){
			fila++;
			
			numColumna = i/10;
			//for(int i = 0; i < numColumnas;i++){			
			grids1.add(new GridBagConstraints());
			grids1.get(grids1.size()-1).gridx = numColumna;
			grids1.get(grids1.size()-1).gridy = fila;
			if(numColumna>0){
				grids1.get(grids1.size()-1).fill = GridBagConstraints.VERTICAL;
				grids1.get(grids1.size()-1).weightx = 1.0;
			}
			aux = i + 1;
			j.add(new JLabel("Ciudad " + aux +": " + Distancias.CIUDADES[c.getGen()[i-1]]),grids1.get(grids1.size()-1));
			if(fila == 9){
				fila = -1;
			}
		}
		grid = new GridBagConstraints();
		grid.gridx = grids1.get(grids1.size()-1).gridx;
		grid.gridy = grids1.get(grids1.size()-1).gridy + 1;
		grid.fill = grids1.get(grids1.size()-1).fill;
		grid.weightx = grids1.get(grids1.size()-1).weightx;
		
		j.add(new JLabel("Ciudad 29: "  + Distancias.CIUDADES[0]),grid);
		
		
		this.add(j);
		
		
		/*for(int i = 0; i < c.getGenes().length;i++){
			this.add(new JLabel("Gen " + i +": " + c.getGenes()[i]));
		}*/
		/*JLabel jLabel4 = new JLabel("FENOTIPO");
		JSeparator jSeparator2 = new JSeparator(SwingConstants.HORIZONTAL);
		this.add(new JLabel("  "));
		this.add(jLabel4);
		this.add(jSeparator2);
		JPanel j2 = new JPanel();
		j2.setLayout(new GridBagLayout());
		ArrayList<GridBagConstraints> grids = new ArrayList<GridBagConstraints>();
		int longitud = c.getFenotipo().length;
		int numColumnas = longitud/10;
		double division = longitud/10;
		division = division-(int)division;
		
		if(division>0){
			numColumnas++;
		}
		numColumna = 0;
		fila = -1;
		for(int i = 0; i < c.getFenotipo().length; i++){
			fila++;
			numColumna = i/10;
			//for(int i = 0; i < numColumnas;i++){			
			grids.add(new GridBagConstraints());
			grids.get(grids.size()-1).gridx = numColumna;
			grids.get(grids.size()-1).gridy = fila;
			if(numColumna>0){
				grids.get(grids.size()-1).fill = GridBagConstraints.VERTICAL;
				grids.get(grids.size()-1).weightx = 1.0;
			}
			j2.add(new JLabel("Fenotipo " + i +": " + c.getFenotipo()[i]),grids.get(grids.size()-1));
			if(fila == 9){
				fila = -1;
			}
		}
		this.add(j2);*/
		/*for(int i = 0; i < c.getFenotipo().length;i++){
			this.add(new JLabel("Fenotipo " + i +": " + c.getFenotipo()[i]));
		}*/
		this.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
	}
	
	

}
